package com.leo.base_project.module.chart;

import android.graphics.Color;
import android.graphics.DashPathEffect;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.Utils;

import java.util.ArrayList;

/**
 * @program: LaserDetection
 * @description: 轨向偏差图表绘制
 * @author: Leo
 * @create: 2021-04-17 16:29
 **/
public class LineChartImpl {

    private LineDataSet set1;
    private LineChart lineChart;

    /*
     * Function 3.2 设置折线图
     * Description 新建一个空的折线图
     * Date 2020/7/24 23:29
     **/
    public LineChart initLineChart(LineChart lineChart) {
        this.lineChart = lineChart;

        //------------------------------------基础设置---------------------------------

        lineChart.setTouchEnabled(true);
        // 可拖曳
        lineChart.setDragEnabled(true);
        // 可缩放
        lineChart.setScaleEnabled(true);
        lineChart.setDrawGridBackground(false);
        lineChart.setPinchZoom(true);
        // 设置图表的背景颜色
        lineChart.setBackgroundColor(Color.TRANSPARENT);
        // 线段数据
        LineData data = new LineData();
        // 数据显示的颜色
        data.setValueTextColor(Color.WHITE);
        // 先增加一个空的数据，随后往里面动态添加
        lineChart.setData(data);
        lineChart.getAxisRight().setEnabled(false);
        lineChart.notifyDataSetChanged();
        lineChart.invalidate();

        //------------------------------------ 设置X轴 ---------------------------------

        XAxis xAxis = lineChart.getXAxis();
        xAxis.enableGridDashedLine(10f, 10f, 0f);
        // 将X轴放在底部
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

        //------------------------------------设置限制线---------------------------------

//        LimitLine ll1, ll2, ll3, ll4;
//        ll1 = new LimitLine(setting.getLeftRightOverDeviation().floatValue(), "轨面超限值:" + setting.getLeftRightOverDeviation()+ "mm");
//        ll4 = new LimitLine(-setting.getLeftRightOverDeviation().floatValue(), "轨面超限值:-" + setting.getLeftRightOverDeviation() + "mm");

//        ll3.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);
//        ll4.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);

        //------------------------------------设置y轴---------------------------------

        YAxis leftAxis = lineChart.getAxisLeft();
        //重置所有限制线,以避免重叠线
        leftAxis.removeAllLimitLines();

//        leftAxis.addLimitLine(ll1);
//        leftAxis.addLimitLine(ll4);

//        //y轴最大
//        leftAxis.setAxisMaximum(50f);
//        //y轴最小
//        leftAxis.setAxisMinimum(-50f);
        leftAxis.enableGridDashedLine(3f, 3f, 0f);
        leftAxis.setDrawZeroLine(false);

        // 限制数据(而不是背后的线条勾勒出了上面)
        leftAxis.setDrawLimitLinesBehindData(true);

        return lineChart;
    }

    /*
     * Function 3.2.1 设置限制线
     * Description 传入LimitLine对象，设置默认形状颜色位置等
     * Date 2020/7/25 16:12
     **/
    public void setLimitLine(LimitLine ll) {
        // LimitLine宽度
        ll.setLineWidth(1f);
        // LimitLine 虚线
        ll.enableDashedLine(10f, 10f, 0f);
        // 标签位置
        ll.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
        // 设置文字大小
        ll.setTextSize(14f);
    }


    /* @Description: 传入折线图lineChart，数据集values，创建或更新折线图数据
     * @Param:Entry（float，float）数组，折线名称
     * @return:
     * @Author: Leo
     * @Date: 2021/4/20
     **/
    public void setData(ArrayList<Entry> values, String type) {
        LineData data;
        if (lineChart.getData() != null && lineChart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) lineChart.getData().getDataSetByIndex(0);
            set1.setValues(values);
            lineChart.getData().notifyDataChanged();
            lineChart.notifyDataSetChanged();
        } else {
            lineChart.getDescription().setText(type);
            set1 = new LineDataSet(values, type);
            // 在这里设置线
            set1.enableDashedLine(10f, 5f, 0f);
            set1.enableDashedHighlightLine(10f, 5f, 0f);
            set1.setColor(Color.BLACK);
            set1.setCircleColor(Color.BLACK);
            set1.setLineWidth(1f);
            set1.setCircleRadius(3f);
            set1.setDrawCircleHole(false);
            set1.setValueTextSize(9f);
            //set1.setDrawFilled(true);
            set1.setFormLineWidth(1f);
            set1.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
            set1.setFormSize(15.f);

            if (Utils.getSDKInt() >= 18) {
                set1.setFillColor(Color.YELLOW);
            } else {
                set1.setFillColor(Color.BLACK);
            }
            ArrayList<ILineDataSet> dataSets = new ArrayList<>();
            //添加数据集
            dataSets.add(set1);

            //创建一个数据集的数据对象
            data = new LineData(dataSets);

            //设置数据
            lineChart.setData(data);

        }
        lineChart.getData().notifyDataChanged();
        lineChart.notifyDataSetChanged();
        lineChart.invalidate();
    }

}
